//
//  ModelView.m
//  FKGW
//
//  Created by song ce on 2017/2/21.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "ModelView.h"
#import "UIView+Extend.h"
//#import "PWContentView.h"
//#import "BottomV.h"
#import "PropertyGroup.h"
#import "PropertyModel.h"
#import "OSSHelper.h"
#import "WaterBtn.h"


@interface ModelView (){
    NSInteger section,row;
}
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSMutableDictionary *valueDic;
@property(nonatomic,strong)NSMutableDictionary *btnDic;
@property(nonatomic,strong)WaterBtn *recordBtn;//记录刚刚添加的btn
@property(nonatomic,copy)NSMutableString *propertyStr;

@end

@implementation ModelView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.dataSource = [NSMutableArray array];
    self.valueDic = [NSMutableDictionary dictionary];
    self.btnDic = [NSMutableDictionary dictionary];
    CGFloat  heigt = Main_Screen_Height-kNaviBarHeight-100-49;
    UIScrollView *scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, heigt-107-20)];
    scrollV.showsVerticalScrollIndicator = NO;
    
    [self.propertyStr appendString:@"请选择"];
}
-(void)initWaterButtonView{
    CGFloat  heigt = Main_Screen_Height-kNaviBarHeight-100-49;
    UIScrollView *scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, heigt-107-20)];
    scrollV.showsVerticalScrollIndicator = NO;
//    UIImage *newImage = [self resizableImage:@"xinxikuang"];
    for(int i=0;i<self.dataSource.count;i++){
        UILabel *nameLabel;//标题
        if(self.recordBtn==nil){
             nameLabel  = [[UILabel alloc]initWithFrame:CGRectMake(16, 8, Main_Screen_Width-32, 21)];
        }else{
            nameLabel  = [[UILabel alloc]initWithFrame:CGRectMake(16, self.recordBtn.bottom+10, Main_Screen_Width-32, 21)];
        }
        PropertyGroup *group = self.dataSource[i];
        [self.propertyStr appendString:@" "];
        [self.propertyStr appendString:group.name];
        nameLabel.text = group.name;
        nameLabel.textColor = DEF_COLOR102;
        nameLabel.font = [UIFont systemFontOfSize:14];
        [scrollV addSubview:nameLabel];
        
                //选项
        CGFloat spaceWith = 10;
        NSMutableArray *titleArr = group.btnArray;
        for (int i = 0; i < titleArr.count; i ++)
        {
            PropertyModel *model = titleArr[i];
            NSString *name = model.value;
            WaterBtn *btn = [WaterBtn buttonWithType:UIButtonTypeCustom];
            btn.titleLabel.font = [UIFont systemFontOfSize:11];
            [btn setTitleColor:DEF_COLOR153 forState:UIControlStateNormal];
            CGRect rect = [name boundingRectWithSize:CGSizeMake(1000 , 15) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:btn.titleLabel.font} context:nil];
            CGFloat BtnW = rect.size.width + 20;
            CGFloat BtnH = 20;
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = BtnH/2;
            if (i == 0){
                btn.frame =CGRectMake(15, nameLabel.bottom+10, BtnW, BtnH);
            }
            else{
                CGFloat yuWidth = Main_Screen_Width  -self.recordBtn.frame.origin.x-self.recordBtn.frame.size.width;
                if (yuWidth >= BtnW+15+spaceWith) {
                    btn.frame =CGRectMake(self.recordBtn.frame.origin.x+self.recordBtn.frame.size.width+ spaceWith, self.recordBtn.top, BtnW, BtnH);
                }else{
                    btn.frame =CGRectMake(15, self.recordBtn.bottom+10, BtnW, BtnH);
                }
            }
            btn.backgroundColor = [UIColor clearColor];
            [btn setTitle:name forState:UIControlStateNormal];
            [scrollV addSubview:btn];
            btn.section = model.section;
//            btn.id = model.id;
            self.recordBtn = btn;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//            [btn setBackgroundImage:newImage forState:UIControlStateNormal];
            [btn setBackgroundColor:DEF_COLOR242];
        }
    }
    scrollV.contentSize  = CGSizeMake(Main_Screen_Width, self.recordBtn.bottom+10);
    [self.modelScrollView addSubview:scrollV];
}

-(void)btnClick:(WaterBtn*)btn{
    NSString *key = [NSString stringWithFormat:@"%ld",btn.section];
//     PropertyGroup *group = self.dataSource[btn.section];
//    NSString *name =group.name;
    NSString *value   = [self.valueDic objectForKey:key];
    if(value.length){
        WaterBtn *preBtn = self.btnDic[key];
        [self delBtn:preBtn];
        [self selBtn:btn];
        [self.valueDic setObject:btn.currentTitle forKey:key];
        [self.btnDic setObject:btn forKey:key];
    }else{
        [self selBtn:btn];
        [self.valueDic setObject:btn.currentTitle forKey:key];
        [self.btnDic setObject:btn forKey:key];
    }
    DLog(@"选中的属性---%@",self.valueDic);
    NSMutableString * p = [[NSMutableString alloc]init];
    for(int i=0;i<self.valueDic.allKeys.count;i++){
        NSInteger index = [self.valueDic.allKeys[i] integerValue];
        PropertyGroup *group = self.dataSource[index];
        NSString *key =group.name;
        NSString *value = self.valueDic[self.valueDic.allKeys[i]];
        if(i==self.valueDic.allKeys.count-1){
           [ p appendString:[NSString stringWithFormat:@"%@:%@",key,value]];
        }else{
            [ p appendString:[NSString stringWithFormat:@"%@:%@;",key,value]];
        }
    }
    BOOL isSelectedAll;
    if(self.valueDic.allKeys.count<self.dataSource.count){
        isSelectedAll = NO;
    }else{
        isSelectedAll = YES;
    }
    if(self.propertyBlock){
        self.propertyBlock(p.copy,isSelectedAll);
    }
}

-(void)selBtn:(WaterBtn*)btn{//选中
    [btn setBackgroundColor:DEF_COLOR];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
-(void)delBtn:(WaterBtn*)btn{//非选中
    [btn setBackgroundColor:DEF_COLOR242];
    [btn setTitleColor:DEF_COLOR153 forState:UIControlStateNormal];
}
- (IBAction)close:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"close" object:nil];
}
-(void)setGoodPic:(NSString *)goodPic{
    _goodPic = goodPic;
    if([_goodPic containsString:@"Documents"]){
         UIImage *image = [[UIImage alloc]initWithContentsOfFile:_goodPic];
        self.goodImageView.image = image;
    }else{
        [[OSSHelper sharedHelper]OSSDownLoadWithObjectKey:_goodPic  xOssProcess:[NSString stringWithFormat:@"image/resize,m_fill,w_%d,h_%d",118,116]  downLoadBlock:^(NSData *data) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.goodImageView.image = [UIImage imageWithData:data];
            });
        }];
    }
}

-(void)setModel:(GoodsDetailModel *)model{
    _model = model;
//    self.firstLabel.text = _model.ppropertyname;
    self.pricelabel.text = [NSString stringWithFormat:@"￥%@",_model.pnowprice];
    self.shuxiangLabel.text =[NSString stringWithFormat:@"%@", _model.ppropertyname];
    if(_model.stocknum>0){
          self.kucunLabel.text = [NSString stringWithFormat:@"库存%@件",_model.stocknum];
    }else{
        self.kucunLabel.text = [NSString stringWithFormat:@"库存%@件",@"0"];
    }
    
    NSArray *nameArr = _model.pproperty;
    if(nameArr.count){
        for(NSDictionary *dic in nameArr){
            NSString *name = dic[@"name"];
            PropertyGroup *group = [[PropertyGroup alloc]init];
            group.name = name;
            NSArray *valueArr = dic[@"values"];
            NSMutableArray *values = [NSMutableArray array];
            for(NSDictionary *ddic in valueArr){
                PropertyModel *model = [[PropertyModel alloc]init];
                model.value = ddic[@"value"];
//                model.id = ddic[@"id"];
                model.section = section;
                [values addObject:model];
            }
            group.btnArray = values;
            [self.dataSource addObject:group];
            section++;
        }
        [self initWaterButtonView];
    }
}

-(UIImage *)resizableImage:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    CGFloat h = image.size.width * 0.5;
    CGFloat w = image.size.height * 0.5;
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(w, h, w, h) resizingMode:UIImageResizingModeStretch];
}
@end
